// pages/launch-check/launch-check.js
var {api, request} = require('../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    show: false,
    showGoods: false,
    showColor: false,
    showArea: false,
    user: '',
    phone: '',
    addr: '',
    p_id:'',
    c_id:'',
    a_id:'',
    area: '',
    pickerTitle: '',
    goodsArr: [],
    goodsColums: [],
    selectedGoodsName: '',
    selectedGoodsIndex: '',
    colorArr: [],
    colorColums: [],
    selectedColorName: '',
    selectedColorIndex: '',
    classfiyArr: [],
    classfiyColums: [],
    selectedClassfiyName: '',
    selectedClassfiyIndex: '',
    storeList: [],
    columns: [],
    storeIndex: '',
    showShare: false,
    storeName: '',
    showClassfiy: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getClassfiy()
    this.getGoods()
    this.getStore()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  showPoup(e) {
    this.setData({
      show: true
    })
  },
  onConfirm(e) {
    this.setData({
      show: false,
      storeIndex: e.detail.index,
      storeName: e.detail.value
    })
  },
  onConfirmClassfiy(e) {
    this.setData({
      showClassfiy: false,
      selectedClassfiyIndex: e.detail.index,
      selectedClassfiyName: e.detail.value
    })
    this.getGoods()
  },
  getStore() {
    request('/store_lst_page', 'POST', {}).then(res => {
      const columns = []
      if (res.data.code == 200) {
        res.data.data.forEach(item => {
          columns.push(item.title)
        })
        this.setData({
          columns: columns,
          storeList: res.data.data
        })
      }
    })
  },
  getClassfiy() {
    request('/get_classfiy_lst', 'POST', {}).then(res => {
      const classfiy = []
      res.data.data && res.data.data.forEach(item => {
        classfiy.push(item.title)
      })
      console.log(classfiy)
      this.setData({
        classfiyArr: res.data.data,
        classfiyColums: classfiy
      })
      console.log(this.data.classfiyColums)
    })
  },
  getGoods() {
    if (!this.data.selectedClassfiyName) return
    request('/goods_lst_page', 'POST', {c_id: this.data.classfiyArr[this.data.selectedClassfiyIndex].id}).then(res => {
      const goodsColums = []
      res.data.data.goods_arr && res.data.data.goods_arr.forEach(item => {
        goodsColums.push(item.title)
      })
      console.log(1)
      this.setData({
        goodsArr: res.data.data.goods_arr,
        goodsColums: goodsColums
      })
    })
  },
  getColor() {
    request('/get_colors_g', 'POST', {
      goods_id:this.data.goodsArr[this.data.selectedGoodsIndex].id
    }).then(res => {
      const colorColums = []
      res.data.data && res.data.data.forEach(item => {
        colorColums.push(item.title)
      })
      this.setData({
        colorArr: res.data.data,
        colorColums: colorColums
      })
    })
  },
  showGoodsPoup(e) {
    if (!this.data.selectedClassfiyName) {
      wx.showToast({
        title: '请先选择系列',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.setData({
      showGoods: true
    })
  },
  onConfirmGoods(e) {
    this.setData({
      showGoods: false,
      selectedGoodsIndex: e.detail.index,
      selectedGoodsName: e.detail.value
    })
    this.getColor()
  },
  onConfirmColor(e) {
    this.setData({
      showColor: false,
      selectedColorIndex: e.detail.index,
      selectedColorName: e.detail.value
    })
    this.getColor()
  },
  showColorPoup(e) {
    if (!this.data.selectedGoodsName) {
      wx.showToast({
        title: '请先选择产品',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.setData({
      showColor: true
    })
  },
  showClassfiyPoup() {
    this.setData({
      showClassfiy: true
    })
  },
  
  close(e){
    this.setData({
      showArea:false,
      area:e.detail.value&&e.detail.value.join(","),
      p_id:e.detail.pid,
      c_id:e.detail.cid,
      a_id:e.detail.aid
    })
  },
  showArea() {
    this.setData({
      showArea: true
    })
  },
  onClose() {
    this.setData({ 
      showGoods: false,
      showColor: false,
      showClassfiy: false,
    });
  },
  onChange() {
    console.log(222)
  },
  onCancel() {
    this.setData({ 
      show: false,
      showGoods: false,
      showColor: false,
    });
  },
  onShareAppMessage: function () {
    return {
      title: '验收',
      desc: '验收',
      path: `/pages/my-check/my-check?id=${this.data.detail.id}&userid=${wx.getStorageSync('user').member_id}`
    }
  },
  submit() {
    const submitData = {
      member_id: wx.getStorageSync('user').member_id,
      user:  this.data.user,
      phone:  this.data.phone,
      addr:  this.data.addr,
      p_id: this.data.p_id,
      c_id: this.data.c_id,
      a_id: this.data.a_id,
      goods_id: this.data.selectedColorName && this.data.goodsArr[this.data.selectedGoodsIndex].id,
      color_id: this.data.selectedColorName && this.data.colorArr[this.data.selectedColorIndex].id,
      store_id: this.data.storeName && this.data.storeList[this.data.storeIndex].id
    }
    if(!submitData.user){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.phone){
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.store_id){
      wx.showToast({
        title: '请选择门店',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.p_id || !submitData.c_id || !submitData.a_id ){
      wx.showToast({
        title: '请输入地区',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.addr){
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.goods_id){
      wx.showToast({
        title: '请选择产品',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.color_id){
      wx.showToast({
        title: '请选择产品颜色',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.showLoading({
      title: '',
    })
    request('/send_acceptance', 'POST', submitData).then(res => {
      wx.hideLoading()
      if (res.data.code != 200) {
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        })
        return;
      }
      wx.showToast({
        title: '提交成功,请点击发送按钮到用户验收',
        icon: 'success',
        duration: 2000
      })
      this.setData({
        detail: res.data.data,
        showShare: true
      })
      // setTimeout(() => {
      //   wx.navigateBack({
      //     delta: 1
      //   })
      // }, 1500)
    })
  }
})